import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
// 什么时候使用路由的懒加载
import Home from '@/views/home/index.vue'
import Kind from '@/views/kind/index.vue'
import Footer from '@/components/Footer.vue'

// 编程式导航时连续点击同一个选项，触发警告
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}
// use
Vue.use(VueRouter)

const routes = [
  { // 路由的匹配是从上到下，匹配到就停止
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home', // 路径
    name: 'home', // 命名路由
    alias: '/h',
    // component: Home
    components: {
      default: Home,
      footer: Footer
    }
  },
  {
    path: '/kind',
    name: 'kind',
    // component: Kind
    components: {
      default: Kind,
      footer: Footer
    }
  },
  {
    path: '/cart',
    name: 'cart',
    // component: () => import('@/views/cart/index.vue')
    components: {
      default: () => import('@/views/cart/index.vue'),
      footer: Footer
    }
  },
  { // 动态路由，name是一个参数的名称
    // ? 代表的就是可选参数name
    path: '/detail/:name?',
    name: 'detail',
    component: () => import('@/views/detail/index.vue')
    // components: {
    //   default: () => import('@/views/detail/index.vue')
    // }
  },
  {
    path: '/user',
    name: 'user',
    // 如果有嵌套路由，那么一定在这个页面有 <router-view></router-view>
    // component: () => import('@/views/user/index.vue'),
    components: {
      default: () => import('@/views/user/index.vue'),
      footer: Footer
    },
    children: [ // 浏览器输入 /user/nologin   以及/user/logined 查看效果
      {
        path: 'nologin', // 嵌套路由不要加 /
        name: 'nologin',
        component: () => import('@/views/user/nologin.vue')
      },
      {
        path: 'logined',
        name: 'logined',
        component: () => import('@/views/user/logined.vue')
      }
    ]
  },
  {
    path: '*',
    component: () => import('@/views/error/404.vue')
  }
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]
console.log(process.env.BASE_URL)
const router = new VueRouter({
  mode: 'history', // hash
  base: process.env.BASE_URL,
  routes
})

export default router
